using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;
using System.Collections.Generic;

public partial class Admin_Categories : SFPage
{
    busCategories insBusCategories = new busCategories();
    entCategories insEntCategories = new entCategories();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgCategories_NeedsDataSource()
    {
        DataTable insDataTableCategories = insBusCategories.SelectCategories();
        if (insBusCategories.HasErrors)
        {
            MessageBoxShow(insBusCategories);
        }
        sfgCategories.SetData(insDataTableCategories);
        return insBusCategories.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgCategories_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntCategories.CategoryId = Convert.ToInt32(e.CommandArgument);
            insBusCategories.DeleteCategoriesByCategoryId(insEntCategories);
            if (insBusCategories.HasErrors)
            {
                MessageBoxShow(insBusCategories);
                return;
            }
            sfgCategories.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstCategories.LoadDataListData();

    }
    protected bool lstCategories_NeedsDataSource()
    {
        insEntCategories.CategoryId = this.Id;
        insBusCategories.SelectCategoriesByCategoryId(insEntCategories);
        if (insBusCategories.HasErrors)
        {
            MessageBoxShow(insBusCategories);
            return true;
        }
        txtCategoryDescription.Text = insEntCategories.CategoryDescription;

        entCategoriesContents insEntCategoriesContents = new entCategoriesContents();
        insEntCategoriesContents.CategoryId = insEntCategories.CategoryId;
        busCategoriesContents insBusCategoriesContents = new busCategoriesContents();
        DataTable insDataTableCategoriesContents = insBusCategoriesContents.SelectCategoriesContentsByCategoryId(insEntCategoriesContents);
        if (insBusCategoriesContents.HasErrors)
        {
            MessageBoxShow(insBusCategoriesContents);
            return true;
        }
        lstCategories.SetData(insDataTableCategoriesContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

            insEntCategories.CategoryDescription = txtCategoryDescription.Text;
        

        foreach (DataListItem dli in lstCategories.Items)
        {
            entCategoriesContents insEntCategoriesContents = new entCategoriesContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntCategoriesContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntCategoriesContents.CategoryName = ((SFTextBox)dli.FindControl("txtCategoryName")).Text;
                if (lstCategories.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntCategoriesContents.CategoryContentId = Convert.ToInt32(lstCategories.DataKeys[dli.ItemIndex]);
                    insEntCategories.CategoriesContentsUpdate.Add(insEntCategoriesContents);
                }
                else
                {
                    insEntCategories.CategoriesContentsInsert.Add(insEntCategoriesContents);
                }
            }
            else
            {
                if (lstCategories.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntCategoriesContents.CategoryContentId = Convert.ToInt32(lstCategories.DataKeys[dli.ItemIndex]);
                    insEntCategories.CategoriesContentsDelete.Add(insEntCategoriesContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusCategories.InsertCategories(insEntCategories);
        if (insBusCategories.HasErrors)
        {
            MessageBoxShow(insBusCategories);
            return;
        }
        base.SaveNew();
        sfgCategories.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntCategories.CategoryId = this.Id;
        insBusCategories.UpdateCategoriesByCategoryId(insEntCategories);
        if (insBusCategories.HasErrors)
        {
            MessageBoxShow(insBusCategories);
            return;
        }
        base.SaveExisting();
        sfgCategories.LoadGridData();
    }

    protected void lstCategories_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["CategoryContentId"] != DBNull.Value);
                e.Item.FindControl("pnlCategoryContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlCategoryContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
